goog.provide('td.Controller');

goog.require('Stefan.Layer');
goog.require('Stefan.Rect');
goog.require('Stefan.Text');
goog.require('Stefan.ui.Button');
goog.require('Stefan.ui.ToggleButton');
goog.require('td.Map');
goog.require('td.Weapon');

td.Controller = T.lang.createClass(function() {
    Stefan.Layer.call(this, document.body, {
    	width: 200,
    	height: 500,
    	left: 600,
    	top: 50
    });
    // this.config = config || {};
    // this.paused = false;
    // this.selectedWeapon = null;
    this.pauseButton = new Stefan.ui.ToggleButton().setText('PAUSE').setSize(70, 35).setPosition(0, 50);
 	this.pauseButton.on('mousedown', function() {
    	if(td.GAME.paused){
    		this.setText('PAUSE');
    		td.GAME.resume();
    		td.GAME.paused = false;
    	}
    	else{
    		this.setText('RESUME');
    		td.GAME.stop();
    		td.GAME.paused = true;
    	}
    });
    // this.upgradeButton = new Stefan.ui.Button().setText('UPGRADE').setSize(70, 35).setPosition(80, 100);
    this.sellButton = new Stefan.ui.Button().setText('SELL').setSize(70, 35).setPosition(0, 100);
    this.sellButton.on('click', function() {
    	var weapon = td.GAME.map.selectWeapon;
    	if(weapon) {
    		var grid = weapon.grid;
    		grid.setFill(grid.defaultColor);
    		grid.removeChild(weapon.body);
    		td.GAME.money += grid.building.cfg.cost;
    		grid.building = null;
    		td.GAME.map.selectWeapon = null;
    		__this.updateMoney();
    	}
    });
    this.restartButton = new Stefan.ui.Button().setText('RESTART').setSize(70, 35).setPosition(80, 50);
    this.restartButton.on('mousedown', function() {
    	location.reload();
    });
    
    this.hpText = new Stefan.Text().setText('生命值: ' + td.GAME.life).setFontColor('rgba(130,130,130,1)').setFontSize(30).setPosition(0, 160).setSize(300, 35);
    this.moneyText = new Stefan.Text().setText('金钱: ' + td.GAME.money).setFontColor('rgba(230,130,130,1)').setFontSize(30).setPosition(0, 200).setSize(300, 35);
    
    this.tip = new Stefan.Rect().setSize(50, 100).setFill('rgba(0,0,0,.5)').hide(true);
    
    this.tipText = new Stefan.Text().setSize(50, 100).setPadding(5).setFontColor('#fff');
    
    this.tip.appendChild(this.tipText);
    
    this.selectMap = new td.Map({
    	columns: 3,
    	rows: 3,
    	gridSize: 50,
    	type: 'control'
    }).setPosition(600, 400);
    
    //武器列表
    this.selectMap.selectWeapons = [];
    var __this = this;
    var c = 0;
    var tipTimeout = null;
    for(var type in td.Weapon.TYPE){
    	var grid = this.selectMap.grids[c];
    	var weapon = new td.Weapon({
    		type: td.Weapon.TYPE[type],
    		map: this.selectMap,
    		grid: grid
    	});
    	weapon.body.on('mousemove', function(event) {
    		clearTimeout(tipTimeout);
    		var p = this.selfToLayer();
    		__this.showTip({x: p.x, y: p.y + 50}, this.info);
    		tipTimeout = setTimeout(function() {
    			__this.tip.hide(true);
    		}, 1000);
    	});
    	this.selectMap.selectWeapons.push(weapon);
    	grid.addWeapon(weapon);
    	grid.on('mousedown', this.onClick);
    	c++;
    }
    this.appendChild(this.pauseButton);
    // this.appendChild(this.upgradeButton);
    this.appendChild(this.sellButton);
    this.appendChild(this.restartButton);
    this.appendChild(this.hpText);
    this.appendChild(this.moneyText);
    this.selectMap.appendChild(this.tip);
    
    this.on('mousemove', function(event) {
    	T.dom.removeStyle(this.getStage().canvas, 'cursor');
    });
}, {
	superClass: Stefan.Layer
}).extend({
	showTip: function(position, text) {
		this.tip.setPosition(position);
		this.tipText.setText(text);
		this.tip.hide(false);
	},
	updateLife: function() {
		this.hpText.setText('生命值: ' + td.GAME.life);
	},
	updateMoney: function() {
		this.moneyText.setText('金钱: ' + td.GAME.money);
	},
	onClick: function(){
		var map = this.map;
		if(map.selectedWeapon && this.building.type == map.selectedWeapon){
			td.Weapon.getMask(td.GAME.getSelectMap().selectedWeapon).hide(true);
			td.GAME.getSelectMap().selectedWeapon = null;
			this.setFill(this.color);
		}
		else {
			if(map.selectedWeapon){
				for(var i = 0; i < map.grids.length; i++){
					map.grids[i].setFill(map.grids[i].color);
				}
			}
			map.selectedWeapon = this.building.type;
			var mask = td.Weapon.getMask(map.selectedWeapon);
			mask.hide(true);
			//If mask is initialized, append it to main map
			if(mask.first)
				td.GAME.map.appendChild(mask);
			this.setFill('rgba(200,0,0,.5)');
		}
	}
});
